package com.rran.study.algorithm.medium.day02;

/**
 * @author yy
 * @Type Solution02.java
 * @Desc 优解
 * @date 2020/9/4
 */
public class Solution02 {

    public static void main(String[] args) {
        String str = "abcgpbcca";
        Solution02 solution02 = new Solution02();
        solution02.lengthOfLongestSubstring(str);
    }

    public int lengthOfLongestSubstring(String s) {
        int[] last = new int[128];
        int start = 0;
        // 返回结果
        int res = 0;
        int n = s.length();
        for (int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index]);
            res = Math.max(res, i - start + 1);
            last[index] = i + 1;
        }
        return res;
    }


}
